<template>
  <div v-loading="loading" class="parentStyle">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('机构全称')" prop="interFullName">
            <el-autocomplete
              v-model="form.interFullName"
              class="inline-input name"
              :fetch-suggestions="companyQuerySearch"
              :placeholder="$t('机构全称')"
              :trigger-on-focus="false"
              :maxlength="100"
              show-word-limit
              @select="companyHandleSelect"
            >
              <template slot="append" @click="companyHandleSelect">
                <a @click="companyHandleSelect">{{ $t('查询机构信息') }}</a>
              </template>
            </el-autocomplete>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('机构简称')" prop="interName">
            <el-input v-model="form.interName" :placeholder="$t('机构简称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('组织形式')" prop="interType">
            <dict-select
              v-model="form.interType"
              dictionary="inter_type"
              :placeholder="$t('组织形式')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('企业统一社会信用代码')" prop="creditCode">
            <el-input v-model="form.creditCode" :placeholder="$t('企业统一社会信用代码')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('注册资本') + $t('金额单位')" prop="capitalRegistered">
            <Money v-model="form.capitalRegistered" :placeholder="$t('注册资本') + $t('金额单位')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('法定代表人')" prop="interRepresentative">
            <el-input v-model="form.interRepresentative" :placeholder="$t('法定代表人')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <!--地域-->
        <Area ref="cityZj" :buss-data="form" />
        <el-col :span="12">
          <el-form-item :label="$t('详细地址')" prop="interAddress">
            <el-input v-model="form.interAddress" :placeholder="$t('详细地址')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('成立时间')" prop="interDate">
            <el-date-picker
              v-model="form.interDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('成立时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系人')" prop="interLinkman">
            <el-input v-model="form.interLinkman" :placeholder="$t('联系人')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系电话')" prop="interPhone">
            <el-input v-model="form.interPhone" :placeholder="$t('联系电话')" show-word-limit maxlength="50" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('联系邮箱')" prop="interEmail">
            <el-input v-model="form.interEmail" :placeholder="$t('联系邮箱')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('公司简介')" prop="companyProfile">
            <el-input v-model="form.companyProfile" type="textarea" :placeholder="$t('公司简介')" show-word-limit maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <!--保存 取消按钮-->
      <save-btn has-permi="base:internal" :cancel-show="!this.$route.query.id" />
    </div>
  </div>
</template>

<script>
    import { saveInternalBase, getInternalBase, checkfullName } from '@/api/fund/internal/internalBase'
    import { Notification } from 'element-ui'
    import { companyListByName, getCompanyByName } from '@/api/company/base/companyBase'
    // 省市组件
    import Area from '@/components/Area'
    export default {
      components: {
        Area
      },
        data() {
            return {
                // 表单参数
                form: {
                  createBy: {
                    id: undefined,
                    nickName: undefined
                  }},
                // 遮罩层
                loading: false,
              // 打开页面的位置，如果是通过项目home页面的路由打开，保存时不调用刷新list页面的方法
                openLocation: undefined,
                // 表单校验
                rules: {
                interFullName: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                interName: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                ],
                  interEmail: [
                    { type: 'email', message: this.$t('邮箱格式错误'), trigger: 'blur' }
                    ]
                },
              // 天眼查查询条件
              tycParam: {
                id: '',
                companyName: '',
                tycId: ''
              },

              // 查询是否存在条件
              existParam: {
                interFullName: '',
                id: ''
              }
            }
        },
        created() {
            this.getDicts('inter_type').then(response => {
                this.interTypeOptions = response.data
            })
        },
        mounted() {
          this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getInternalBase(data.id).then(response => {
                        this.form = response.data
                        this.$nextTick(() => {
                        if (this.$refs?.cityZj?.initCity !== undefined) {
                          this.$refs.cityZj.initCity()
                          this.$refs.cityZj.initDistrict()
                        }
                    })
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,

                    interFullName: undefined,

                    interName: undefined,

                    interType: undefined,

                    creditCode: undefined,

                    capitalRegistered: undefined,

                    interRepresentative: undefined,

                    provinceRegistered: undefined,

                    cityRegistered: undefined,
                    districtRegistered: undefined,

                    interAddress: undefined,

                    interDate: undefined,

                    interLinkman: undefined,

                    interPhone: undefined,

                    interEmail: undefined,

                    companyProfile: undefined,

                    delFlag: undefined,

                    createBy: undefined,

                    createTime: undefined,

                    updateBy: undefined,

                    updateTime: undefined,

                    detailsRegistered: undefined,
                    setDate: undefined,
                    tycId: undefined

                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveInternalBase(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                })
           },
          // 标的公司查询
          companyQuerySearch(queryString, cb) {
            companyListByName(queryString).then(res => {
              if (res.code === 200) {
                cb(res.data)
              }
            })
          },
          // 标的公司选择后事件
          companyHandleSelect(item) {
            this.loading = true
            const interFullName = this.form.interFullName
            if (interFullName === '' || interFullName === undefined) {
              Notification.error({
                title: this.$t('请先输入机构全称！')
              })
              this.loading = false
              return
            }
            this.resetForm()
            this.tycParam.id = this.form.id
            this.tycParam.tycId = item.id
            this.tycParam.companyName = interFullName
            this.existParam.interFullName = interFullName
            this.existParam.id = this.form.id
            checkfullName(this.existParam).then(response => {
              if (response.data) {
                this.form.interFullName = ''
                this.msgError(this.$t('该机构全称已存在!'))
                this.loading = false
              } else {
                this.tycParam.tycId = item.id
                this.tycParam.companyName = interFullName
            getCompanyByName(this.tycParam).then(res => {
              if (res.code === 200) {
                if (res.msg === 'nofind') {
                  this.form.companyName = undefined
                  Notification.error({
                    title: this.$t('天眼查没有获取到该机构信息！')
                  })
                  this.loading = false
                } else {
                  // 公司简称
                  this.form.interName = interFullName
                  // 公司全称
                  this.form.interFullName = interFullName
                  // 企业统一社会信用代码
                  this.form.creditCode = res.data.creditCode
                  // 法人代表
                  this.form.interRepresentative = res.data.legalPerson
                  // 成立时间
                  this.form.interDate = res.data.setDate
                  // 注册地-省
                  this.form.provinceRegistered = res.data.provinceRegistered
                  this.form.cityRegistered = res.data.cityRegistered

                  if (this.$refs?.cityZj?.initCity !== undefined) {
                    this.$refs.cityZj.initCity()
                    this.$refs.cityZj.initDistrict()
                  }
                  // 注册区
                  this.form.districtRegistered = res.data.districtRegistered
                  // 注册地-详细地址
                  this.form.interAddress = res.data.detailsRegistered
                  // 注册资本金额
                  this.form.capitalRegistered = res.data.capitalRegistered
                  // 天眼查ID
                  this.form.tycId = res.data.tycId
                  // 关联公司ID
                  this.form.companyId = res.data.id
                  this.msgSuccess(this.$t('已获取机构信息！'))
                  this.loading = false
                }
              }
            })
              }
            })
          },
          // 初始化天眼查获取的数据
          resetForm() {
            this.form.interFullName = undefined
            this.form.interName = undefined
            this.form.creditCode = undefined
            this.form.interrepresentative = undefined
            this.form.createTime = undefined
            this.form.provinceRegistered = undefined
            this.form.cityRegistered = undefined
            this.form.detailsRegistered = undefined
            this.form.capitalRegistered = undefined
            this.form.establishTime = undefined
            this.form.setDate = undefined
            this.form.tycId = undefined
            this.form.companyId = undefined
          }

        }

    }
</script>
